package com.zxb.base.monitor.vo;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zxb.base.monitor.util.LayoutUtil;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author Zxb
 */
@Data
@Builder
@Accessors(chain = true)
public class Cpu {
    /**
     * 可用处理器数
     */
    private int availableProcssors;

    /**
     * 系统CPU使用率
     */
    private BigDecimal systemCpuUsage;

    /**
     * 当前进程CPU使用率
     */
    private BigDecimal processCpuUsage;

    @Override
    public String toString() {
        return LayoutUtil.Table.of(
                LayoutUtil.Row.of("Available Processors", availableProcssors),
                LayoutUtil.Row.of("System CPU Usage", NumberUtil.mul(systemCpuUsage, 100).setScale(2, RoundingMode.HALF_UP) + "%"),
                LayoutUtil.Row.of("Process CPU Usage", NumberUtil.mul(processCpuUsage, 100).setScale(2, RoundingMode.HALF_UP) + "%")
        ).toString();
    }

    public JSONArray toJSON() {
        JSONArray ja = new JSONArray();
        JSONObject jo = new JSONObject();
        jo.put("availableProcessors", availableProcssors);
        jo.put("systemCpuUsage", NumberUtil.mul(systemCpuUsage, 100).setScale(2, RoundingMode.HALF_UP) + "%");
        jo.put("processCpuUsage", NumberUtil.mul(processCpuUsage, 100).setScale(2, RoundingMode.HALF_UP) + "%");
        ja.add(jo);
        return ja;
    }
}
